<template>
  <div class="stock-info" @click.stop="jump">
    <div class="left">
      <div class="stock-name">{{ stockname }}</div>
      <div class="stock-code">{{ stockcode }}</div>
    </div>
    <div class="right">
      <div class="num" v-redGreen="price || nowPrice">
        {{ (price || nowPrice) | formatPrice }}
      </div>
      <div class="num increase" v-redGreen="price || nowPrice">
        {{ (increase || nowIncrease) | formatIncrease }}
      </div>
      <img
        src="//i.thsi.cn/staticS3/mobileweb-upload-static-server.img/m/atom/d0ff5aaa-28f0-4215-9197-05854e59db01.png"
        alt=""
      />
    </div>
  </div>
</template>

<script>
import { jumpToFenShi, formatNumber, getMarketInfoTable } from '@/scripts/tools.js';
export default {
  props: {
    stockcode: {
      type: String,
      default: '',
    },
    stockname: {
      type: String,
      default: '',
    },
    price: {
      type: String,
      default: '',
    },
    increase: {
      type: String,
      default: '',
    },
    market: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      // 当前股票没有涨停的时候使用现价和涨停
      nowPrice: '',
      nowIncrease: '',
    };
  },
  methods: {
    jump() {
      jumpToFenShi(this.stockcode, this.market);
    },
    getStockInfo() {
      getMarketInfoTable({ stockcodes: [this.stockcode], marketids: [this.market], tableData: [{}] }).then(resTable => {
        this.nowIncrease = resTable[0].upRise;
        this.nowPrice = resTable[0].price;
      });
    },
  },
  mounted() {
    this.getStockInfo();
  },
  filters: {
    formatIncrease(num) {
      if (num === '' || isNaN(num)) {
        return '--';
      }
      return formatNumber(+num, '+M.2%');
    },
    formatPrice(num) {
      if (num === '' || isNaN(num)) {
        return '--';
      }
      return formatNumber(+num, 'M.2');
    },
  },
};
</script>
<style lang="less" scoped>
.stock-info {
  width: 100vw;
  height: 104px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #ffffff;
  padding: 16px 32px;
  box-sizing: border-box;
  .left {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .stock-name {
      font-family: PingFangSC-Regular;
      font-size: 32px;
      color: rgba(0, 0, 0, 0.84);
      letter-spacing: 0;
      line-height: 40px;
      font-weight: 400;
    }
    .stock-code {
      font-family: PingFangSC-Regular;
      font-size: 24px;
      color: rgba(0, 0, 0, 0.4);
      line-height: 32px;
      font-weight: 400;
    }
  }
  .right {
    display: flex;
    align-items: center;
    .increase {
      margin-left: 60px;
    }
    .num {
      font-family: THS;
      font-size: 32px;
      color: #ff2436;
      letter-spacing: 0;
      line-height: 40px;
      font-weight: 500;
    }
    img {
      width: 32px;
      height: 32px;
      margin-left: 20px;
    }
  }
}
</style>
